package com.camunda.workflow.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.camunda.workflow.domain.entity.SysMenu;
import com.camunda.workflow.mapper.SysMenuMapper;
import com.camunda.workflow.service.SysMenuService;
import com.camunda.workflow.utils.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public SysMenu getById(Long id) {
        return sysMenuMapper.selectOneById(id);
    }

    @Override
    public Page<SysMenu> getPage(SysMenu req) {
        if (ObjUtil.isNull(req)) {
            req = new SysMenu();
        }
        Page<SysMenu> page = new Page<>(req.getPageNum(), req.getPageSize());
        QueryWrapper wrapper = QueryWrapper.create()
                .where(SysMenu::getId).eq(req.getId(), ObjUtil.isNotNull(req.getId()))
                .where(SysMenu::getName).eq(req.getName(), ObjUtil.isNotNull(req.getName()))
                .where(SysMenu::getAuth).eq(req.getAuth(), ObjUtil.isNotNull(req.getAuth()))
                .orderBy(req.toMybatisFlexOrders());
        return sysMenuMapper.paginate(page, wrapper);
    }

    @Override
    public List<SysMenu> getAll(SysMenu req) {
        if (ObjUtil.isNull(req)) {
            req = new SysMenu();
        }
        QueryWrapper wrapper = QueryWrapper.create()
                .where(SysMenu::getId).eq(req.getId(), ObjUtil.isNotNull(req.getId()))
                .where(SysMenu::getName).eq(req.getName(), ObjUtil.isNotNull(req.getName()))
                .where(SysMenu::getAuth).eq(req.getAuth(), ObjUtil.isNotNull(req.getAuth()))
                .orderBy(req.toMybatisFlexOrders());
        return sysMenuMapper.selectListByQuery(wrapper);
    }

    @Override
    public boolean saveAndEdit(SysMenu req) {
        if (ObjUtil.isNull(req)) {
            return false;
        }
        return saveOrUpdate(req);
    }

    @Override
    public boolean deleteByIds(List<Long> ids) {
        return removeByIds(ids);
    }
}
